package com.note.simpleshop.module.goods.service;

import com.note.simpleshop.entity.TbGoods;
import com.note.simpleshop.module.goods.dao.GoodsRepository;
import com.note.simpleshop.module.goods.exception.GoodsException;
import com.note.simpleshop.module.goods.vo.GoodsDetailVo;
import com.note.simpleshop.module.goods.vo.GoodsSpecVo;
import com.note.simpleshop.module.goods.vo.SpecVo;
import com.note.simpleshop.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsService {
    private static final String CACHE_NAME="goods_cache";
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private SpecService specService;
    public PageBean<TbGoods> findByPage(Integer pageNumber, String classyifyId, String name) {
        if (pageNumber == null) {
            pageNumber = 0;
        }
        else
        {
            pageNumber=pageNumber-1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = new PageRequest(pageNumber, 20, sort);
        TbGoods tbGoods = new TbGoods();
        tbGoods.setIsDelete(TbGoods.IS_DELEETE0);
        tbGoods.setSale(TbGoods.SALE1);
        ExampleMatcher matcher = ExampleMatcher.matching();
        if (StringUtils.isNotBlank(classyifyId)) {
            tbGoods.setClassifyId(classyifyId);
            matcher.withMatcher("classifyId", ExampleMatcher.GenericPropertyMatchers.contains());
        }
        if (StringUtils.isNotBlank(name)) {
            tbGoods.setName(name);
            matcher.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        }
        Example<TbGoods> example = Example.of(tbGoods, matcher);
        Page<TbGoods> page = goodsRepository.findAll(example, pageRequest);
        int total=(int)page.getTotalElements();
        PageBean<TbGoods> pageBean=new PageBean(page.getTotalPages(),total,page.isFirst(),page.isEmpty(),page.getNumber(), page.getContent());;
        return pageBean;
    }
    /***
     *
     * 获取某个商品的信息
     * @param id
     * @return
     */
    public TbGoods findById(String id)
    {
        return goodsRepository.getOne(id);
    }
    @Cacheable(CACHE_NAME)
    public TbGoods findCacheById(String id)
    {
        return findById(id);
    }
    public GoodsDetailVo findDetailOne(String id) throws GoodsException {
        GoodsDetailVo goodsDetailVo=new GoodsDetailVo();
        TbGoods tbGoods=goodsRepository.getOne(id);
        if(tbGoods==null)
        {
            throw new GoodsException("商品不存在");
        }
        //产品信息
        goodsDetailVo.copy(tbGoods);
        String goodsId=tbGoods.getId();
        //具体的某个商品，如果鞋子，颜色：红色，大小：43
        List<GoodsSpecVo> goodsSpecs=goodsSpecService.findByGoodsId(goodsId);
        goodsDetailVo.copyGoodsSpec(goodsSpecs);
        //商品的规格
        List<SpecVo>  specVoList=specService.findByGoodsId(goodsId);
        goodsDetailVo.copySpecVo(specVoList);
        return goodsDetailVo;
    }
}
